/**
 ******************************************************************************
 * @file    Project/STM32F10x_StdPeriph_Template/main.c
 * @author  MCD Application Team
 * @version V3.6.0
 * @date    20-September-2021
 * @brief   Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2011 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>

#include "./SYSTEM/systick/systick.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/MPU6050/mpu6050.h"
#include "./BSP/MPU6050/inv_mpu.h"
#include "./BSP/MPU6050/inv_mpu_dmp_motion_driver.h"

/** @addtogroup STM32F10x_StdPeriph_Template
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/**
 * @brief  Main program.
 * @param  None
 * @retval None
 */
int main(void) {
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_stm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f10x.c file
       */

    /* Add your application code here
     */
    uint8_t res;
    float pitch, roll, yaw;    // 欧拉角
    short aacx, aacy, aacz;    // 加速度传感器原始数据
    short gyrox, gyroy, gyroz; // 陀螺仪原始数据
    short temp;                // 温度

    Systick_Init();
    usart1_init();

    MPU_Init();

    while (1) {
        res = mpu_dmp_init();
        if (res == 0)
            break;
        else
            printf("MPU init failed, res: %d\n", res);
    }

    /* Infinite loop */
    while (1) {
        if (mpu_dmp_get_data(&pitch, &roll, &yaw) == 0) {
            temp = MPU_Get_Temperature();               // 得到温度值
            MPU_Get_Accelerometer(&aacx, &aacy, &aacz); // 得到加速度传感器数据
            MPU_Get_Gyroscope(&gyrox, &gyroy, &gyroz);  // 得到陀螺仪数据

            // Send_ANOTC();
            printf("Pitch:  %f\t", (float)pitch);
            printf("Roll:  %f\t", (float)roll);
            printf("yaw:  %f\n", (float)yaw);
            printf("temp:  %f\r\n", (float)temp);

            delay_ms(100);
        }
    }
}

#ifdef USE_FULL_ASSERT

/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t* file, uint32_t line) {
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1) {
    }
}
#endif

/**
 * @}
 */
